<?php
class WxPay extends Base
{
    private $params;
    const PKURL = "https://fraud.mch.weixin.qq.com/risk/getpublickey";
    const BANKPAY = "https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank";
    public function getPuyKey(){
        $this->params = [
        'mch_id'    => self::MCHID,//商户ID
        'nonce_str' => md5(time()),
        'sign_type' => 'MD5'
        ];
        //将数据发送到接口地址
        return $this->send(self::PKURL);
    }
    public function bankPay($data){
        $this->params = [
        'mch_id'              => self::MCHID,//商户号
        'partner_trade_no'    =>$data['partner_trade_no'], //订单号
        'nonce_str'           => md5(time()), //随机串
        'enc_bank_no'         => $data['enc_bank_no'],//收款方银行卡号RSA加密
        'enc_true_name'       => $data['enc_true_name'],//收款方姓名RSA加密
        'bank_code'           => $data['bank_code'],//收款方开户行
        'amount'              => $data['amount'],//付款金额
        ];
        //将数据发送到接口地址
        return $this->send(self::BANKPAY);
    }
    //签名
    public function sign(){
        return $this->setSign($this->params);
    }
    public function send($url){
        $res = $this->sign();
        $xml = $this->ArrToXml($res);
        $returnData = $this->postData($url, $xml);
        return $this->XmlToArr($returnData);
    }

    /**
     * 公钥加密，银行卡号和姓名需要RSA算法加密
     * @param string $data    需要加密的字符串
     * @return null|string    加密后的字符串
     */
    public function publicEncrypt($data = '',$pubkey='')
    {
        if (!is_string($data)) {
            return null;
        }
        // 进行加密
        $r = openssl_public_encrypt($data,$encrypt_data,$pubkey,OPENSSL_PKCS1_OAEP_PADDING);
        if($r){//加密成功，返回base64编码的字符串
            return base64_encode(''.$encrypt_data);
        }else{
            return false;
        }
    }



}
